﻿using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure;
using MediatR;
using S.Y.Passport.Write.API.Applications.Commands;

namespace B.S.BaseData.Read.API.Application.CommandHandler
{
    public class XiangGoodsQueryCommandHandler : IRequestHandler<XiangGoodsQueryCommand, ApiResult<GoodsInfo>>
    {
        /// <summary>
        /// 商品信息仓储
        /// </summary>
        private readonly IBaseRepository<GoodsInfo> goodsInfoRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="goodsInfoRepository"></param>
        public XiangGoodsQueryCommandHandler(IBaseRepository<GoodsInfo> goodsInfoRepository)
        {
            this.goodsInfoRepository = goodsInfoRepository;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<GoodsInfo>> Handle(XiangGoodsQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<GoodsInfo> result = new ApiResult<GoodsInfo>();

            var obj = goodsInfoRepository.GetAll().FirstOrDefault(x => x.GoodsId == request.GoodsId);
            if (obj == null)
            {
                result.Code = ApiEnums.查询失败;
                result.Msg = ApiEnums.查询失败.ToString();
            }
            else
            {
                result.Code = ApiEnums.查询成功;
                result.Msg = ApiEnums.查询成功.ToString();
                result.Data = obj;
            }

            return Task.FromResult(result); 
        }
    }
}
